package de.tub.bazinga.object;

import processing.core.PApplet;
import de.tub.bazinga.main.Bazinga;
import de.tub.bazinga.object.interf.Drawable;

public class Timer implements Drawable {

	private static final long GAME_DURATION = 60000;
	private static final int TEXT_SIZE = 80;

	private long startTime = 0;
	private boolean isRunning = false;

	// Callback um das Spiel zu stopen wenn die Zeit vorbei ist.
	private Bazinga game = null;

	public Timer(Bazinga game) {
		super();
		this.game = game;
	}

	@Override
	public void draw(PApplet window) {
		if (isRunning) {

			long timeDifference = System.currentTimeMillis() - startTime;

			if (timeDifference < GAME_DURATION) {
				Integer timeToDisplay = (int) ((GAME_DURATION - timeDifference) / 1000);

				int x = window.getWidth() - 105;
				int y = 85;

				window.fill(245);
				window.textSize(TEXT_SIZE);
				window.text(timeToDisplay, x, y);
			} else {
				game.stopGame();
				isRunning = false;
			}

		}
	}

	public void start() {
		startTime = System.currentTimeMillis();
		isRunning = true;
	}

	public void stop() {
		startTime = 0l;
		isRunning = false;
	}

	public boolean isRunning() {
		return isRunning;
	}

	public void setRunning(boolean isRunning) {
		this.isRunning = isRunning;
	}

}
